package com.cn.delan.web.mvc.admin.action;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import com.cn.delan.core.util.DateUtil;
import com.cn.delan.core.util.StringUtil;
import com.cn.delan.web.mvc.admin.AdminConst;
import com.cn.delan.web.mvc.admin.bean.FileBean;
import com.cn.delan.web.mvc.core.action.AbstractAction;
/**
 * 资源管理器
 * @author BingGo
 *
 */
public class ExplorerAction extends AbstractAction {

	@Override
	public ModelAndView handle(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String path = this.getParameter(AdminConst.REQUEST_PARA_PATH);
		String filename = this.getParameter(AdminConst.REQUEST_PARA_FILENAME);
		path = path==null?this.getServletContext().getRealPath(""):path;
		//搜索文件名
		request.setAttribute(AdminConst.REQUEST_ATTR_FILENAME, filename);
		//路径格式化
		request.setAttribute(AdminConst.REQUEST_ATTR_PATH, path.replaceAll("\\\\", "\\\\\\\\"));
		//获取特定路径下的文件列表
		FileBean []fileBean = null;
		if(StringUtil.isEmpty(filename)){
			//显示当前目录下的文件
			fileBean = this.listFiles(path);
		}else{
			//查找该目录下的文件
			fileBean = this.searchFiles(path,filename);
		}
		request.setAttribute(AdminConst.REQUEST_ATTR_FILES, fileBean);
		//获取根目录下的文件列表
		FileBean []rootBean = this.listRoots();
		request.setAttribute(AdminConst.REQUEST_ATTR_ROOTS, rootBean);
		//获取面包屑路径文件列表
		FileBean []parentBean = this.listParents(path);
		request.setAttribute(AdminConst.REQUEST_ATTR_PARENTS, parentBean);

		request.setAttribute(AdminConst.REQUEST_ATTR_FILE, this.getMessage("admin.file"));
		request.setAttribute(AdminConst.REQUEST_ATTR_FOLDER, this.getMessage("admin.folder"));
		request.setAttribute(AdminConst.REQUEST_ATTR_SHOW, this.getMessage("admin.show"));
		request.setAttribute(AdminConst.REQUEST_ATTR_HIDDEN, this.getMessage("admin.hidden"));
		return this.getView(INPUT);
	}
	private FileBean[] listRoots(){
		File roots[] = File.listRoots();
		FileBean []rootBean = this.transfer(roots);
		return rootBean;
	}
	private FileBean[] listFiles(String path){
		File directory = new File(path);
		File []files = directory.listFiles();
		FileBean []fileBean = this.transfer(files);
		return fileBean;
	}
	private FileBean[] searchFiles(String path,String filename){
		File directory = new File(path);
		ArrayList<File> list = this.searchFiles(new ArrayList<File>(), directory, filename);
		File []files = list.toArray(new File[list.size()]);
		FileBean []fileBean = this.transfer(files);
		return fileBean;
	}
	private ArrayList<File> searchFiles(ArrayList<File> list,File file,String filename){
		ArrayList<File> result = list;
		if(result==null){
			result = new ArrayList<File>();
		}
		//判断文件是否符合搜索要求
		if(file.getName().indexOf(filename)!=-1){
			result.add(file);
		}
		if(file.isDirectory()){
			//发现目录递归查询其子目录
			for(File subFile:file.listFiles()){
				result = searchFiles(result,subFile,filename);
			}
		}
		return result;
	}
	private FileBean[] listParents(String path){
		ArrayList<File> list = new ArrayList<File>();
		File parent = new File(path);
		while(parent!=null){
			list.add(0,parent);
			parent = parent.getParentFile();
		}
		FileBean []parentBean = this.transfer(list.toArray(new File[list.size()]));
		return parentBean;
	}
	/**
	 * 将文件对象转换为特定的文件bean
	 * @param files
	 * @return
	 */
	private FileBean[] transfer(File []files){
		if(files==null){
			return null;
		}
		FileBean []fileBean = new FileBean[files.length];
		for(int i=0;i<files.length;i++){
			fileBean[i] = new FileBean();
			if(files[i].getParentFile()!=null){
				fileBean[i].setParentPath(files[i].getParentFile().getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
			}
			fileBean[i].setPath(files[i].getAbsolutePath().replaceAll("\\\\", "\\\\\\\\"));
			fileBean[i].setName(files[i].getName());
			fileBean[i].setSize((int)(files[i].length()/1024/1024));
			fileBean[i].setDirectory(files[i].isDirectory());
			fileBean[i].setHidden(files[i].isHidden());
			Date date = new Date(files[i].lastModified());
			fileBean[i].setLastmodify(DateUtil.toString(date, "yyyy-MM-dd HH:mm:ss"));
		}
		return fileBean;
	}

}
